/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-7-15 下午5:32 Created by dwzhang
 */
package com.jade.bss.service.map.baidu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jade.bss.base.BssException;
import com.jade.bss.service.map.Location;
import com.jade.bss.service.map.MapService;
import com.jade.bss.service.map.baidu.pdu.BaiduLocation;
import com.jade.bss.service.map.baidu.pdu.BaiduResponse;
import com.jade.framework.base.util.HttpUtils;
import com.jade.modules.region.Region;
import com.jade.modules.region.RegionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

/**
 * 百度位置服务接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-7-15 下午5:32
 */
@Named ("baidu_mapService")
public class BaiduService
        implements MapService
{
    protected ObjectMapper objectMapper = new ObjectMapper();
    private String url = "http://api.map.baidu.com/geocoder/v2/";
    private String key = "1c46142f7c209fe4c225bf41b339674a";
    private String dataFormat = "json";

    public String getKey()
    {
        return key;
    }

    public void setKey(String key)
    {
        this.key = key;
    }

    public String getDataFormat()
    {
        return dataFormat;
    }

    public void setDataFormat(String dataFormat)
    {
        this.dataFormat = dataFormat;
    }

    @PostConstruct
    public void init()
    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public Location getLocation(String city, String address)
            throws BssException
    {
        if (StringUtils.isNumeric(city)) {
            Region region = RegionUtils.getRegion(city);
            if (region != null) {
                city = region.getCnName();
            }
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ak", key));
        params.add(new BasicNameValuePair("output", dataFormat));
        params.add(new BasicNameValuePair("city", city));
        params.add(new BasicNameValuePair("address", address));
        String strParam = URLEncodedUtils.format(params, "UTF-8");

        try {
            BaiduResponse r = HttpUtils.postHttp(url, params, BaiduResponse.class);
            Location location = new Location();
            if (r.getStatus() == 0) {
                BaiduLocation l = r.getResult().getLocation();
                if (l != null) {
                    location.setLatitude(l.getLatitude());
                    location.setLongitude(l.getLongitude());
                }
                return location;
            }
            else {
                return null;
            }
        }
        catch (Exception ex) {
            throw new BssException(ex.getMessage(), ex.getMessage(), ex);
        }
    }
}
